package za.co.chaotik.tron.patterns;

import java.util.ArrayList;
import java.util.List;


public class Subject {
	private List<Observer> observers;
	
	public Subject() {
		observers = new ArrayList<Observer>();
	}
	
	public void addObserver(Observer observer) {
		if (!observers.contains(observer)) {
			observers.add(observer);
			observer.hasChanged(this);
		}
	}
	
	public void removeObserver(Observer observer) {
		observers.remove(observer);
	}
	
	/**
	 * Notifies all registered observers that this subject's state has changed.
	 */
	public void changed() {
		for (Observer observer : observers) {
			observer.hasChanged(this);
		}
	}
}
